﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;

namespace fbasync
{
    [DataContract]
    [Flags]
    public enum ExtendedPermission : long
    {
        [EnumMember]
        none = 0,
        [EnumMember]
        status_update = 1,
        [EnumMember]
        photo_upload = 2,
        [EnumMember]
        sms = 4,
        [EnumMember]
        [Obsolete]
        create_listing = 8,
        [EnumMember]
        offline_access = 16,
        [EnumMember]
        email = 32,
        [EnumMember]
        create_event = 64,
        [EnumMember]
        rsvp_event = 128,
        [EnumMember]
        publish_stream = 512,
        [EnumMember]
        bookmarked = 1024,
    }

    [DataContract(), Serializable()]
    public class ExtendedPermissions
        : FacebookEntity
    {
        public readonly static IEnumerable<string> AllFields =
            new string[] { "uid", "status_update", "photo_upload", "sms", /*"create_listing",*/ "offline_access", "email",
                           "create_event", "rsvp_event", "publish_stream", "bookmarked" };

        public const string DefaultOrderBy = "uid";
        public const string FacebookEntity = "permissions";

        [DataMember]
        public long? uid { get; set; }

        [DataMember]
        public bool? status_update { get; set; }

        [DataMember]
        public bool? photo_upload { get; set; }

        [DataMember]
        public bool? sms { get; set; }

        [DataMember]
        [Obsolete]
        public bool? create_listing { get; set; }

        [DataMember]
        public bool? offline_access { get; set; }

        [DataMember]
        public bool? email { get; set; }

        [DataMember]
        public bool? create_event { get; set; }

        [DataMember]
        public bool? rsvp_event { get; set; }

        [DataMember]
        public bool? publish_stream { get; set; }

        [DataMember]
        public bool? bookmarked { get; set; }

        public ExtendedPermission ToEnum()
        {
            var @enum = ExtendedPermission.none;
            if (status_update.GetValueOrDefault()) @enum |= ExtendedPermission.status_update;
            if (photo_upload.GetValueOrDefault()) @enum |= ExtendedPermission.photo_upload;
            if (sms.GetValueOrDefault()) @enum |= ExtendedPermission.sms;
#pragma warning disable 612
            if (create_listing.GetValueOrDefault()) @enum |= ExtendedPermission.create_listing;
#pragma warning restore 612
            if (offline_access.GetValueOrDefault()) @enum |= ExtendedPermission.offline_access;
            if (email.GetValueOrDefault()) @enum |= ExtendedPermission.email;
            if (create_event.GetValueOrDefault()) @enum |= ExtendedPermission.create_event;
            if (rsvp_event.GetValueOrDefault()) @enum |= ExtendedPermission.rsvp_event;
            if (publish_stream.GetValueOrDefault()) @enum |= ExtendedPermission.publish_stream;
            if (bookmarked.GetValueOrDefault()) @enum |= ExtendedPermission.bookmarked;

            return @enum;
        }

        public static IList<ExtendedPermissions> FromFqlResult(XmlDocument fqlResult, bool keepXml)
        {
            List<ExtendedPermissions> results = fqlResult.GetFqlResults(keepXml, e =>
                new ExtendedPermissions
                {
                    uid = e.GetChildLong("uid"),
                    create_event = e.GetChildBool("create_event"),
                    create_listing = e.GetChildBool("create_listing"),
                    email = e.GetChildBool("email"),
                    offline_access = e.GetChildBool("offline_access"),
                    photo_upload = e.GetChildBool("photo_upload"),
                    rsvp_event = e.GetChildBool("rsvp_event"),
                    sms = e.GetChildBool("sms"),
                    status_update = e.GetChildBool("status_update"),
                    publish_stream = e.GetChildBool("publish_stream"),
                    bookmarked = e.GetChildBool("bookmarked"),
                });
            return results;
        }
    }
}
